package com.lzxuni.service.impl;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lzxuni.po.*;
import com.lzxuni.service.CreateTableService;
import com.lzxuni.service.SheBeiService;
import com.lzxuni.utils.Chongzushuju;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.mapper.ForwardMapper;
import com.lzxuni.mapper.QxDataMapper;
import com.lzxuni.service.QxDataService;
import com.lzxuni.utils.MethodUtil;
import com.lzxuni.utils.UrlUtil;
import com.lzxuni.utils.UuidUtil;


@Service("qxDataService")
public class QxDataServiceImpl implements QxDataService {

	@Autowired
	private QxDataMapper qxDataMapper ;
	@Autowired
	private ForwardMapper forWardMapper ;
	@Autowired
	private CreateTableService createTableService;
	@Autowired
	private SheBeiService sheBeiService;

	@Override
	public void insertQxData(String data,String userkey,String date) throws Exception {

		SimpleDateFormat sdf=new SimpleDateFormat("yyMMddHHmmss");
		Map<String,Object> map=new HashMap<String,Object>();
		String createTime=MethodUtil.formatDate(0,sdf.parse(date));

		//根据userkey确定接口数据类型，重组数据字符串
		String newdata= Chongzushuju.NewString(userkey,data);


		String[] dataArr= newdata.split(",");
		//String[] dataArr= data.split(",");

		System.out.println("createTime-----------"+createTime);

		map.put("id", UuidUtil.get32UUID());
		map.put("date",createTime );
		map.put("userkey", userkey);
		map.put("data", dataArr);
		String tableName = "tbl_"+userkey;

		CreateTable createTable = new CreateTable();
		createTable.setTableName(tableName);

		//查询数据库是否已经拥有当前设备数据（旧版）
//		List<CreateTable> listTable = createTableService.findCreateTable(tableName);
//		if(listTable.size()==0){
//			createTableService.createTable(createTable);
//			createTable.setTableId(UuidUtil.get32UUID());
//			createTableService.addTableName(createTable);
//			map.put("tableName",createTable.getTableName());
//			qxDataMapper.addQxData(map);
//		}else{
//			map.put("tableName",createTable.getTableName());
//			List<QxData> findByCreateTime = qxDataMapper.findQxDataByCreateTime(map);
//			if(findByCreateTime.size()==0){
//				qxDataMapper.addQxData(map);
//			}
//		}

		//新版
		List<SheBei> listShebei = sheBeiService.findSheBei(tableName);
		SheBei shebei=new SheBei();
		shebei.setTableName(tableName);
		shebei.setUserkey(userkey);
		if(listShebei.size()==0){
			System.out.println("come to createtable and addshebei-------"+userkey);

			createTableService.createTable(createTable);
			shebei.setId(UuidUtil.get32UUID());

			sheBeiService.addSheBei(shebei);
			map.put("tableName",shebei.getTableName());
			qxDataMapper.addQxData(map);
		}else{
			System.out.println("come to updatetable and update shebei-------"+userkey);

			map.put("tableName",shebei.getTableName());
			List<QxData> findByCreateTime = qxDataMapper.findQxDataByCreateTime(map);
			if(findByCreateTime.size()==0){
				qxDataMapper.addQxData(map);
				sheBeiService.updateSheBeiLastTime(userkey);
			}else{
				System.out.println("------数据时间重复------");
			}
		}


		//数据转发到其他项目
		Map<String, String> map1=new HashMap<String, String>();
		map1.put("data", data);
		map1.put("userkey", userkey);
		map1.put("date", createTime);
		UrlUtil util=new UrlUtil();
		String url="";
		ForwardCostom forwardCostom=new ForwardCostom();
		forwardCostom.setUserkey(userkey);
		List<ForwardCostom> forwardCostoms=forWardMapper.findForwardList(forwardCostom);
		for(int i=0;i<forwardCostoms.size();i++){
			url=forwardCostoms.get(i).getForwardUrl();
			if(url!=null&&url.length()>0){
				util.get(url, map1);
			}
		}

		System.out.println("完成");

	}

	@Override
	public Integer findQxDataInspect(String userkey, String createTime) {
		return qxDataMapper.findQxDataInspect(userkey, createTime);
	}

	@Override
	public QxData findQxDataById(String id) throws Exception {
		return qxDataMapper.findQxDataById(id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<?> findQxDataList(int pageNumber, int pageSize,QxDataCostom qxDataCostom) throws Exception {
		PageHelper.startPage(pageNumber, pageSize).setOrderBy("createTime desc");
		List<QxDataCostom> list = qxDataMapper.findQxDataList(qxDataCostom);

		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}

	@Override
	public List<QxDataCostom> findQxDataList(QxDataCostom qxDataCostom) throws SQLException{
		return qxDataMapper.findQxDataList(qxDataCostom);
	}

	@Override
	public PageInfo<?>  findQxGroupList(int pageNumber, int pageSize,QxDataCostom qxDataCustom)
			throws SQLException {
		PageHelper.startPage(pageNumber, pageSize).setOrderBy("userkey desc");
		List<QxDataCostom> list = qxDataMapper.findQxGroupList(qxDataCustom);
		PageInfo pageInfo = new PageInfo(list);
		return pageInfo;
	}
}
